﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Chat : MonoBehaviour
{
    public List<GameObject> textChat;
    //public List<GameObject> faceChat;
    public Transform grid;
    public GameObject smiles;
    private void Awake()
    {
        UIEventListener.Get(smiles).onClick = ButtonClick;
        for (int i = 0; i < 59; i++)
        {
            GameObject obj = GameObject.Instantiate(smiles);

            if (i > 7)
            {
                obj.name = (i + 2).ToString();
            }
            else
            {
                obj.name = "0" + (i + 2).ToString();
            }
            obj.GetComponent<UISprite>().spriteName = "emotion_0" + obj.name;
            obj.transform.SetParent(grid);

            UIEventListener.Get(obj).onClick = ButtonClick;
        }
        for (int i = 0; i < textChat.Count; i++)
        {
            UIEventListener.Get(textChat[i]).onClick = ButtonClick;
        }

        //for (int i = 0; i < faceChat.Count; i++)
        //{
        //    UIEventListener.Get(faceChat[i]).onClick = ButtonClick;
        //}
    }

    void ButtonClick(GameObject button)
    {
        if (button.name.Contains("t"))
        {
            //文字聊天
            RoomChatType RCT = RoomChatType.TextId;
            Send(RCT, button.name.Substring(0, 1));
        }
        else
        {
            RoomChatType RCT = RoomChatType.MovieClip;
            Send(RCT, button.name);
        }
    }

    void Send(RoomChatType RCT, string content)
    {
        RoomChatRequest lg = new Pb.RoomChatRequest { MsgType = RCT, Content = content };
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(lg, MsgId.RoomChatRequest);
        Destroy(gameObject);
    }

    public void DestroySelf()
    {
        Destroy(gameObject);
    }
}
